<?

/*	CalendarField OBJECT
*	Comment here
*	
*					*/


class CalendarField extends modTEXT {

	function __construct( $vars ) {
		
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	var $days = array(
		"nl"=>	array( "Ma", "Di", "Wo", "Do", "Vr", "Za", "Zo" )
	);
	
	public function output() {
	
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();
	
	
		return $this->parse( $result ) . $this->render_js();
	}
	
	function ajax() {
	
		if( $this->action )
			$result = $this->{ "ajax_" . $this->action }();
			
		return $result;
	
	}
	
	private function filter_mini() {
	
		$this->addClass( "mini" );
		$grid[] = $this->render_grid();
		
		$this->js = "
			mod = new CalendarField( ". json_encode( $this->vars ) .", ". json_encode( $this->filter_load( 0 )) ." );
			mod.type_uri = ". json_encode( $this->filter_type_uri() ) .";
		";

		return implode( "\n", $grid );
	}
	
	private function filter_big() {
	
		$this->class = "big";
		$grid[] = $this->render_grid();
		
		$this->js = "
			mod = new CalendarField( ". json_encode( $this->vars ) .", ". json_encode( $this->filter_load( 0 )) ." );
			mod.type_uri = ". json_encode( $this->filter_type_uri() ) .";
		";

		return implode( "\n", $grid );
	}
	
	private function filter_load( $current ) {
		global $DB;
		
		$date = explode( ".", date( "m.N.n.t.Y.j.F", strtotime("+". $current ." month")));
		
		$set = array( "month"=> $date[6], "days"=> (int) $date[3], "today"=> (int) $date[5] );
		$set[ "first" ] 	= 1 - (int) date( "N", mktime( 0, 0, 0, $date[2], 1, $date[4]));
		$set[ "prevdays" ]	= (int) date( "t", mktime( 0, 0, 0, $date[2]-1, 1, $date[4]));
		$set[ "prevstr" ]	= date( "m Y", strtotime("+". ($current-1) ." month"));
		$set[ "curstr" ]	= $date[0] ." ". $date[4];
		$set[ "nextstr" ]	= date( "m Y", strtotime("+". ($current+1) ." month"));
		
		$datestr = date( "Y-m-d", mktime( 0, 0, 0, $date[2], 1+$set['first'], $date[4]));
		
		$keys	= array( "ID", "objid, otype, location, datediff( date,'$datestr') as diff, date_format(date, '%k.%i') as hour, <%lang:(select mo.nameid from mod_object as mo where mo.ID=objid)%> as name" );
		$query	= "datediff( date, '$datestr') <= 42 && datediff( date, '$datestr') >= 0 && active = 1 && (select mo.active from mod_object as mo where mo.ID=objid && mo.active=1) order by date asc";
		
		if( $dates = $DB->filter_key( "cal.single", $query, $keys ))
			foreach( $dates as $key => $node ) {
				if( !$set[ "locations" ][ $node['location']])  $set[ "locations" ][ $node['location']] = $DB->get_key( "location", "ID=". $node['location'], "<%lang:nameid%> as name" );
				
				$dates[ $key ]['uri'] = "/o/". $DB->type_r( $node['otype'] ) ."/". urlencode( $node['name'] ) . "/" .$node['objid'];
				if( $node['hour'] < "06.00" ) $dates[ $key ]['diff'] = (int) $dates[ $key ]['diff'] -1;
				$set[ "numref" ][ $dates[ $key ]['diff'] ][] = $key;
			}


		$set[ "nodes" ] = $dates;
		return $set;
		
		/*$dates = $DB->parse("mo.ID, c.otype, DATEDIFF(c.date,'".$date['query']."') as diff, if(char_length(tx.short)>32, concat(substr(tx.short,1,24),'...'), tx.short) as name, ".$DB->_lang("mo.textid","`long`")." as content, date_format(c.date, '%e/%c/%Y') as date","mod_object as mo, txtdump as tx, mod_calendar as c","DATEDIFF(c.date,'".$date['query']."') <= 42 and DATEDIFF(c.date,'".$date['query']."') >= 0 and c.ctype in(3,4) and c.otype in(1,2,5,7,8,9) and mo.ID=c.objid and tx.textid=mo.nameid and tx.lang='".LANG."' and mo.active order by c.date, mo.rank",true);
		
		if($dates) foreach($dates as $dte) {
			$dte['content'] = substr(stripslashes(strip_tags($dte['content'])), 0, ($ajx->vars->multi)?42:246 );
			$datejs["refcal.".($dte['diff']+1)][] = $dte;
		} else $datejs = array();



		foreach($datejs as $k=>$dte) {
		if(count($dte)>1){ unset($alts); unset($djs); $wcount=0;
			foreach($dte as $n => $obj){
				
				if($obj['otype']==2 && count($dte)>1){
					unset($datejs[$k][$n], $dte[$n]); $wcount+=0.75;
					$djs[]="<a href='artists/bio?id=".$obj['ID']."'>".$obj['name']."</a>";
				} else {
					$keep = $n; $wcount++;
					$alts[]="<a href='artists/bio?id=".$obj['ID']."' class='alt'>".$obj['name']."</a>";
				}
			}
			$dte[$keep]['content'] = substr($dte[$keep]['content'], 0, 246-$wcount*24 );
			
			$dte[$keep]['alt'] = implode("<br />", $alts);
			if($djs) $dte[$keep]['alt'].= "<br />".implode(", ", $djs);
		} else $keep = count($dte)-1;
		
		$datejs[$k][0] = $dte[$keep];
		
		
	}

		
		
		*/
	}
	
	private function filter_type_uri() {
		global $DB;
		
		$types = $DB->filter_key( "cal.single", "active", "distinct otype" );
		foreach( $types as $id )
			$type_uri[ $id['otype'] ] = "/o/". $DB->type_r( $id['otype'] ) ."/";
			/*if( $pid = json_decode( $DB->type_r( $id['otype'], "meta" ))->pageid ) {
				$page = new PAGE( $pid );
				$type_uri[ $id['otype'] ] = $page->parse_uri();
			}*/
		
		return $type_uri;
	}
	
	private function render_grid() {
	
		$grid[] = "<div class='header'></div>";
		foreach( $this->days[ LANG ] as $day ) $nodes .= "<span>$day</span>";
		$grid[] = "<div class='days'>$nodes</div>";
		
		while( $n++ < 6 ) { 
			$nodes = "";
			for( $y =0; $y < 7; $y++ ) $nodes.= "<span class='node'></span>";
			$grid[] = $nodes;
		}
		
		return "<div class='grid'>". implode( "\n", $grid ) ."</div>";

	}
	
	
	
	/////////////////////////////////////
	/////// MODULE AJAX CALLS (js output)
	
	function ajax_mini() {
		// THIS ONE GETS PARSED, SEND JSON STRINGS ONLY. 
		global $DB;
		
		/*if( $group = $DB->reference( $this->vars->parenttype, "parentid = ".$DB->smartq( $this->vars->id, "int" ), $this->vars->objtype ))
			$images = $DB->filter_key( $this->vars->objtype, "ID in(". implode( ",", $group ) .")", "ID as id" );*/
		$json = $this->filter_load( $this->vars->current );
		
		return json_encode( $json );
	}

} 

/*


	
	
	
	
	if(!$ajx->vars->multi) foreach($datejs as $k=>$dte) {
		if(count($dte)>1){ unset($alts); unset($djs); $wcount=0;
			foreach($dte as $n => $obj){
				
				if($obj['otype']==2 && count($dte)>1){
					unset($datejs[$k][$n], $dte[$n]); $wcount+=0.75;
					$djs[]="<a href='artists/bio?id=".$obj['ID']."'>".$obj['name']."</a>";
				} else {
					$keep = $n; $wcount++;
					$alts[]="<a href='artists/bio?id=".$obj['ID']."' class='alt'>".$obj['name']."</a>";
				}
			}
			$dte[$keep]['content'] = substr($dte[$keep]['content'], 0, 246-$wcount*24 );
			
			$dte[$keep]['alt'] = implode("<br />", $alts);
			if($djs) $dte[$keep]['alt'].= "<br />".implode(", ", $djs);
		} else $keep = count($dte)-1;
		
		$datejs[$k][0] = $dte[$keep];
		
		
	}


*/


/*
$MOD->class.= " refcal";
	$body = "<script src='http://central.onedotonly.be/js/obj.calendar.js' type='text/javascript'></script>";
	if(!$_set->multi) $body.= "<div class='info' id='refcal.info'><div id='refcal.info.title' class='title'></div><div id='refcal.info.date' class='date'></div><div id='refcal.info.content' class='content'></div></div>";
	else $MOD->class.= " multi";
	
	$body.= "
	<table cellspacing='1' cellpadding='0'><tbody id='refcal.tbody'>
		<tr class='cheader'><td colspan='7'>
			<img id='refcal.rgt' src='".THEME."img/rarr.png' class='arrow' style='float:right;' alt='next' /><img id='refcal.lft' src='".THEME."img/larr.png' class='arrow' style='float:left;' alt='back' /><span id='refcal.month'>wakker worden</span>
		</td></tr>
		<tr class='cfirst'><td>Ma</td><td>Di</td><td>Wo</td><td>Do</td><td>Vr</td><td>Za</td><td>Zo</td></tr>";
	$n = -1;
	while( $n++ < 5 ) {
		$body.= "<tr>"; $y = 0;
		while( $y<7 ) $body.= "<td class='blur' id='refcal.".($n*7 + ++$y)."'></td>";
		$body.= "</tr>";
	} 
	$body.= "</tbody></table>
	
	<script type='text/javascript'>
		calendar.multi = ".(($_set->multi)?1:0)."; 
		calendar.load(0);
		$('refcal.lft').observe('click', function(){ calendar.load(-1); });
		$('refcal.rgt').observe('click', function(){ calendar.load(1); });";
	if(!$_set->multi) $body.="\n$('refcal.month').observe('mouseover', calendar.refer);";
	$body.= "</script>";

*/
?>